// Created by liusc on 2020/9/17--11:17.
import pager from '@/assets/vue-mixin/pager'
import sortTree from '@/components/sort-tree'
import initiateAdd from '../form/addition_add.vue'
import initiateEdit from '../form/addition_edit.vue'
import initiateLook from '../form/addition_look.vue'
import {mapGetters} from "vuex"
import {fetchList, addObj, putObj, getObj, delObj, judgeInitiate} from "@/views/wy-budget/api/budgetadd.js";
import {getDict} from "~/utils";

export default {
    name: 'resource-manage',
    mixins: [pager],
    components: {sortTree, initiateAdd, initiateEdit, initiateLook},
    data() {
        return {
            tableData: [],
            searchForm: {},
            get_button: false,
            add_button: false,
            edit_button: false,
            del_button: false,
            placeholder: '请输入预算编号或者申请名称',
        }
    },

    computed: {
        ...mapGetters(['permissions']),
    },

    created() {
    },

    mounted(){
        this.get_button = this.permissions['addition_get']
        this.add_button = this.permissions['addition_add']
        this.edit_button = this.permissions['addition_edit']
        this.del_button = this.permissions['addition_del']
    },

    methods: {

        searchByKey(val){
            this.searchForm.budgetType = 13
            if (val) {
                this.searchForm.budgetCode = val
            } else {
                delete this.searchForm.budgetCode
            }
            this.resetList()
        },

        getListData() {
            this.searchForm.budgetType = 13
            fetchList(Object.assign({}, this.pageParams, this.searchForm)).then(resp => {
                this.setListData(resp.data)
            })
        },

        /**
         * 跳转到新增页面
         * @param scope
         */
        toAddData() {

            // 判断当年是否存在预算，存在则不能添加
            // setTimeout(() => {
            //     //通过id获取项目信息
            //     judgeInitiate().then(respData => {
            //         console.log(respData)
            //         if(respData.data != null && respData.data.data.length > 0){
            //             console.log(respData)
            //             this.$alert("预算已存在，不能重复生成");
            //         }else{
            //             this.$refs.initiateAddInfo.show();
            //         }
            //
            //     }).catch(e => {
            //         this.$message({
            //             type: 'error',
            //             message: '数据获取失败!'
            //         })
            //     })
            // }, 200)

            this.$refs.initiateAddInfo.show();
        },

        /**
         * 新增保存
         * @param data
         */
        addData(data) {
            addObj(data).then((data)=>{
                this.$alert('操作完成', '提示', {
                    confirmButtonText: '确定',
                    // callback: action => {
                    //     this.$message({
                    //         type: 'info',
                    //         message: `action: ${ action }`
                    //     });
                    // }
                });
                this.resetList();
            })
        },

        /**
         * 跳转到编辑页面
         * @param scope
         */
        toEditData(scope) {
            getObj(scope.row.budgetId).then(respData => {
                this.$refs.initiateEditInfo.show(respData.data.data);
            })
        },

        /**
         * 跳转到编辑页面
         * @param scope
         */
        toLookData(scope) {
            getObj(scope.row.budgetId).then(respData => {
                this.$refs.initiateLookInfo.show(respData.data.data);
            })
        },

        /**
         * 修改保存
         * @param data
         */
        updateData(data) {
            putObj(data).then((data)=>{
                this.$alert('操作完成', '提示', {
                    confirmButtonText: '确定',
                    // callback: action => {
                    //     this.$message({
                    //         type: 'info',
                    //         message: `action: ${ action }`
                    //     });
                    // }
                });
                this.resetList();
            })
        },

        /**
         * 根据主键ID删除
         * @param scope
         */
        deleteRow(scope) {
            this.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {

                delObj(scope.row.budgetId).then(response => {
                    this.resetList()
                })

                this.$message({
                    type: 'success',
                    message: '删除成功!'
                });
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });
            });
        },

    }
}
